package com.zhuge_new.Activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.maning.mndialoglibrary.MToast;
import com.zhuge_new.Base.ActivityExKt;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.PreferencesUtils;
import com.zhuge_new.untils.TimerUtil;

import org.json.JSONObject;

/**
 * Created by lenovo on 2018-09-12.
 */

public class PasswordActivity extends BaseActivity implements View.OnClickListener {

    private EditText pass_code;
    private EditText pass_name;
    private EditText pass_card;
    private EditText pass_set;
    private TextView get_code;
    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        init_title();
    }

    @Override
    public void init_title() {
        super.init_title();
        TextView tv_title=findViewById(R.id.tv_nav_title);
        ImageView iv_back=findViewById(R.id.iv_nav_back);
        TextView pass_phone=findViewById(R.id.pass_phone);
        pass_code = findViewById(R.id.pass_code);
        pass_name = findViewById(R.id.pass_name);
        pass_card = findViewById(R.id.pass_card);
        pass_set = findViewById(R.id.pass_set);
        Button btn_pass=findViewById(R.id.btn_pass);
        get_code = findViewById(R.id.get_code);
        phone = PreferencesUtils.getString(this,"cell_phone");
        pass_phone.setText(phone.substring(0,3)+"****"+ phone.substring(7, phone.length()));
        tv_title.setText("设置交易密码");
        btn_pass.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        get_code.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popOneActivity(PasswordActivity.class);
                break;
            case R.id.btn_pass:
                if (pass_name.getText().toString().isEmpty()){
                    MToast.makeTextShort(baseContext,"姓名不能为空，请重新设置").show();
                }else if (pass_card.getText().toString().isEmpty()){
                    MToast.makeTextShort(baseContext,"身份证号不能为空，请重新设置").show();
                }else if (pass_set.getText().toString().isEmpty()){
                    MToast.makeTextShort(baseContext,"您设置新的交易密码小于六位，请重新设置").show();
                }else if (pass_code.getText().toString().isEmpty()){
                    MToast.makeTextShort(baseContext,"您输入的验证码小于六位，请重新设置").show();
                }else if (pass_code.getText().toString().length()!=6){
                    MToast.makeTextShort(baseContext,"您输入的验证码小于六位，请重新设置").show();
                }else {
                    pass_reset();
                }
                break;
            case R.id.get_code:
                TimerUtil timerUtil=new TimerUtil(get_code,60000,1000);
                timerUtil.start();
                getCode();
                break;
        }

    }

    //修改支付密码提交
    private void pass_reset() {
        OkGo.<String>post(BaseHttp.paypawd_change_mobile)
                .tag(this)
                .headers("token",PreferencesUtils.getString(baseContext,"token"))
                .params("password",pass_set.getText().toString())
                .params("mobile",phone)
                .params("smsCode",pass_code.getText().toString())
                .params("CAAC_Certificate",pass_card.getText().toString())
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        if (msgCode.equals("100")){
                            MToast.makeTextShort(baseContext,msg).show();
                            ActivityStack.getScreenManager().popOneActivity(PasswordActivity.class);
                        }
                    }
                });
    }

    //发送验证码
    public void getCode() {
        OkGo.<String>post(BaseHttp.identify_othercode_password)
                .tag(this)
                .headers("token",PreferencesUtils.getString(baseContext,"token"))
                .params("mobile",phone)
                .params("type","1")
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        if (msgCode.equals("100")){
                            MToast.makeTextShort(baseContext,msg).show();
                        }
                    }
                });
    }
}
